<?php

namespace App\V3\V3_2_6\Models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class OverflowBreakagePackageModel extends Model
{


    use HasFactory;

    protected $table = 'overflow_breakage_package';
    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储

    //toArray 时间转换问题
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    static public function getMainId($keyword, $data)
    {
        $result = self::select(
            'o_b_main_id'
        )
            ->from('overflow_breakage_package as a')
            ->leftJoin('package as b', 'b.id', '=' ,'a.package_id')
            ->leftJoin('overflow_breakage as d', 'd.id', '=', 'a.o_b_main_id')
            ->where('a.main_id', $data['main_id'])
            ->where('d.type', $data['type'])
            ->where('d.repeal_status', $data['repeal_status'])
            ->where('b.package_name', 'like', '%'.$keyword.'%')
            ->distinct()
            ->get()->toArray();

        return $result;
    }

    public function packageContent()
    {
        return self::hasMany(OverflowBreakagePackageContentModel::class, 'o_b_p_id', 'id');
    }
}
